<template>
  <div class="footer-page">
    <el-form ref="form" :rules="rules" :model="form" label-width="160px">
      <el-form-item v-if="parentId" label="上级分类:">
        <el-select v-model="form.parentid" placeholder="请选择上级分类">
          <el-option v-for="item in typeList" :key="item.id" :label="item.type_name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="信息配置名称:" prop="type_name">
        <el-input v-model="form.type_name" />
      </el-form-item>
      <el-form-item label="排序:">
        <el-input-number v-model="form.sort" />
      </el-form-item>
      <el-form-item label="状态:">
        <el-radio v-model="form.status" :label="1">显示</el-radio>
        <el-radio v-model="form.status" :label="2">隐藏</el-radio>
      </el-form-item>
    </el-form>
    <div class="page-footer-btns">
      <el-button size="small" @click="resetForm">重置</el-button>
      <el-button size="small" type="primary" @click="submitForm">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getInfoTypeList, addOrEditInfoConfig, getInfoConfig } from '@/api/web'
export default {
  // props: ['parentId', 'editId'],
  props: {
    parentId: {
      type: String,
      default: ''
    },
    editId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      typeList: [],
      form: {
        parentid: 0,
        type_name: '',
        sort: 0,
        status: 1
      },
      rules: {
        type_name: [
          { required: true, message: '请输入信息配置名称', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    // console.log(this.parentId, this.editId);
    this.getList()
    if (this.editId) {
      this.$route.meta.title = '编辑信息'
      this.getData()
    } else {
      this.$route.meta.title = '添加信息'
    }
  },
  methods: {
    // 编辑获取信息
    getData() {
      getInfoConfig(this.editId).then(res => {
        this.form = res
      })
    },
    // 表单提交
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          addOrEditInfoConfig(this.form).then(res => {
            if (!this.editId) {
              // 新增跳转列表页
              this.$router.push({ name: 'articleInfoConfig' })
            }
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    },
    // 重置
    resetForm() {
      this.$refs['form'].resetFields()
      if (this.editId) {
        this.getData()
      } else {
        this.form = {
          parentid: this.parentId ? this.parentId : 0,
          type_name: '',
          sort: 0,
          status: 1
        }
      }
    },
    // 获取下拉列表
    getList() {
      getInfoTypeList().then(res => {
        this.typeList = res
        if (this.parentId) {
          this.form.parentid = parseInt(this.parentId)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
